package com.project.petLossSystem.config;

import com.project.petLossSystem.common.Constants;
import com.project.petLossSystem.interceptor.AdminLoginInterceptor;
import com.project.petLossSystem.interceptor.UserLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置类
 */
@Configuration
public class PetLossSystemWebMvcConfigurer implements WebMvcConfigurer {
    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;
    @Autowired
    private UserLoginInterceptor userLoginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // admin拦截
        registry.addInterceptor(adminLoginInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/css/**")
                .excludePathPatterns("/admin/img/**")
                .excludePathPatterns("/admin/js/**")
                .excludePathPatterns("/admin/webfonts/**");
        // user拦截
        registry.addInterceptor(userLoginInterceptor)
                .addPathPatterns("/profile/**")
                .addPathPatterns("/post/createPost/**")
                .addPathPatterns("/post/delete/**")
                .addPathPatterns("/contact/**")
                .addPathPatterns("/searchImg/**");
    }

    // 配置资源处理器的方法，通常在Spring MVC的配置类中使用。它的作用是将指定的URL路径映射到本地文件系统上的资源位置。
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // file是说明指定的是文件
        registry.addResourceHandler(Constants.CAROUSEL_URL + "**").addResourceLocations("file:" + Constants.File_IMAGE_UPLOAD_DIC + Constants.CAROUSEL_URL);
        registry.addResourceHandler(Constants.PROFILE_URL + "**").addResourceLocations("file:" + Constants.File_IMAGE_UPLOAD_DIC + Constants.PROFILE_URL);
        registry.addResourceHandler(Constants.POST_URL + "**").addResourceLocations("file:" + Constants.File_IMAGE_UPLOAD_DIC + Constants.POST_URL);
        registry.addResourceHandler(Constants.MATCH_URL+"**").addResourceLocations("file:" + Constants.File_IMAGE_UPLOAD_DIC + Constants.MATCH_URL);
        registry.addResourceHandler(Constants.PROFILE_USER_URL + "**").addResourceLocations("file:" + Constants.File_IMAGE_UPLOAD_DIC + Constants.PROFILE_USER_URL);
    }
}
